// pages/odd/index.js
var url = getApp().globalData.ApiUrl;
var timeformat = require('../../utils/timeformat.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hr: '',   //小时
    min: '', //分钟
    sec: '', //秒钟
    kanjia_add_time: [],   // 砍价时间
    timeShow: true
  },
  count_down: function (allsec) {
    var that = this;
    // 渲染倒计时时钟
    that.setData({
      day: timeformat.timeformat(allsec).days + '天',
      hr: timeformat.timeformat(allsec).hrs,
      min: timeformat.timeformat(allsec).mins,
      sec: timeformat.timeformat(allsec).second
    });
    if (allsec <= 0) {
      that.setData({
        day: '0天',
        hr: '00',
        min: '00',
        sec: '00'
      });
      // timeout则跳出递归
      return;
    };
    setTimeout(function () {
      // 放在最后--
      allsec -= 1000;
      that.count_down(allsec);
    }, 1000);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getMyBargain();
    // if (options.share) {
    //   that.share = false;
    //   wx.navigateTo({
    //     url: '/pages/joinbargain/index?buyer_id=' + that.buyer_id + '&goods_id=' + that.goods_id + '&kanjia_id=' + that.kanjia_id,
    //   });
    // }
  },
  //我的砍价
  getMyBargain: function (){
    var that = this;
    wx.showLoading({
      title: '加载中，请稍后...',
    });
    wx.request({
      url: url + '/mobile/index.php?act=api&op=my_kanjia_list',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'get',
      data: {
        key: wx.getStorageSync('key'),
      },
      success: function (res){
        if(res.data.code == 200){
          wx.hideLoading();
          var mybargain = res.data.datas;
          var kanjia_time = [];
          var total_time = [];
          that.setData({
            mybargain: mybargain,
          }); 
          console.log(mybargain);         
          for(var i in mybargain){
            var had_kanjia_price = 0;
            for (var j in mybargain[i].kanjia_price.kan_price){
              had_kanjia_price += parseFloat(mybargain[i].kanjia_price.kan_price[j]);
            }  
            kanjia_time.push(that.timeFormat(mybargain[i].add_time * 1000))
            mybargain[i].kanjia_price.had_kanjia_price = had_kanjia_price;  
            var now = new Date().getTime();
            total_time.push(mybargain[i].end_time * 1000 - now);  // 砍价限定时间
            if (total_time[i] <= 0) {
              that.setData({
                timeShow: false
              })
            }
            that.setData({
              kantotal: mybargain[i].kanjia_price.had_kanjia_price,
              total_time: total_time,
            });
          };
          that.setData({
            kanjia_add_time: kanjia_time,
          });
          
          that.count_down(total_time)       
        }else{
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: function (){
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        });
      }
    });
  },
  // 邀请好友
  toJoinInfo: function(e) {
    var buyer_id = e.currentTarget.dataset.buyer_id;
    var kanjia_id = e.currentTarget.dataset.kanjia_id;
    var order_id = e.currentTarget.dataset.order_id;
    var goods_id = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/my-bargain/index?buyer_id=' + buyer_id + '&goods_id=' + goods_id + '&order_id=' + order_id + '&kanjia_id=' + kanjia_id,
    })
  },
  // 查看我的砍价
  shareTap: function(e) {
    var that = this;
    var buyer_id = e.currentTarget.dataset.buyer_id;
    var kanjia_id = e.currentTarget.dataset.kanjia_id;
    var order_id = e.currentTarget.dataset.order_id;
    var goods_id = e.currentTarget.dataset.goods_id;
    that.setData({
      buyer_id: buyer_id,
      kanjia_id: kanjia_id,
      order_id: order_id,
      goods_id: goods_id
    });
    wx.navigateTo({
      url: '/pages/my-bargain/index?buyer_id=' + buyer_id + '&goods_id=' + goods_id + '&order_id=' + order_id + '&kanjia_id=' + kanjia_id,
    })
  },
  /**
   * 时间戳转出时间格式
   */
  timeFormat: function (timestamp){
    var that = this;
    var time = new Date(timestamp);
    var year = time.getFullYear();
    var month = time.getMonth() + 1;
    var date = time.getDate();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    return year + '-' + that.add0(month) + '-' + that.add0(date) + ' ' + that.add0(hours) + ':' + that.add0(minutes) + ':' + that.add0(seconds);
  },
  add0: function (m){
    return m < 10 ? '0' + m : m
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
  
  }
  
})